package org.unit.testmgmt.ui.generator.wizard;

import java.util.Vector;

import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.ui.JavaElementLabelProvider;
import org.eclipse.jdt.ui.StandardJavaElementContentProvider;
import org.eclipse.jface.viewers.CheckboxTreeViewer;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.dialogs.ContainerCheckedTreeViewer;
import org.unit.testmgmt.generator.wizard.GeneratorWizardMain;
import org.unit.testmgmt.messages.Messages;
import org.unit.testmgmt.tml.Testprio;
import org.unit.testmgmt.tml.UTMModel;

/**
 * The page for the method-selection and the main settings.
 * 
 * @author Robert Streng
 *
 */
public class GeneratorWizardMainPage extends GeneratorWizardBasePage {

	public GeneratorWizardMainPage(String title, String description,
			String pageName, UTMModel model) {
		super(title, description, pageName, model);
	}

	private String generateTestsuites;

	private CheckboxTreeViewer checkboxTreeViewer;
	private Text superClass;
	private Button btnNamefilter;
	private Button btnModifierfilter;
	private Button btnFailassertion;

	private Button btnLogger;
	private Text methodPrefix;
	private Text txtTestProject;
	private Button btnPrioHigh;
	private Button btnPrioStandard;
	private Button btnPrioLow;

	private Vector<IMethod> methods;
	private Testprio selectedTestprio = Testprio.DEFAULT;
	private Button btnSelectAll;
	private Button btnDeselectAll;

	private Button btnExistingMethodsFilter;

	private Button btnSetupbeforeclass;

	private Button btnTeardown;

	private Button btnTeardownafterclass;

	private Button btnToggleStandardMethods;

	private Button btnTestsuites;

	private Button btnToggleOther;

	private Button btnToggleHooks;

	private Button btnCreatetestbasebefore;

	private Button btnBeforemethodcall;
	
	private Button btnAftermethodcall;
	
	private Button btnSetup;
	
	private Button btnCreatetestbaseafter;
	
	public Button getBtnCreatetestbasebefore() {
		return btnCreatetestbasebefore;
	}

	
	public Button getBtnCreatetestbaseafter() {
		return btnCreatetestbaseafter;
	}

	public void setBtnCreatetestbaseafter(Button btnCreatetestbaseafter) {
		this.btnCreatetestbaseafter = btnCreatetestbaseafter;
	}

	public void setBtnCreatetestbasebefore(Button btnCreatetestbasebefore) {
		this.btnCreatetestbasebefore = btnCreatetestbasebefore;
	}

	public void setBtnCreatetestbase(Button btnCreatetestbase) {
		this.btnCreatetestbasebefore = btnCreatetestbase;
	}

	@Override
	public void createControl(Composite parent) {
		Composite mainContainer = new Composite(parent, SWT.NONE);
		setControl(mainContainer);
		mainContainer.setLayout(new FillLayout(SWT.VERTICAL));

		Group grpNewMethods = new Group(mainContainer, SWT.NONE);
		grpNewMethods.setText(Messages.GeneratorWizardMainPage_methods);
		grpNewMethods.setLayout(new FillLayout(SWT.HORIZONTAL));

		SashForm sashForm = new SashForm(grpNewMethods, SWT.NONE);
		Composite composite = new Composite(sashForm, SWT.FILL);
		composite.setLayout(new FillLayout(SWT.HORIZONTAL));

		ScrolledComposite scrolledComposite = new ScrolledComposite(composite,
				SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
		scrolledComposite.setExpandVertical(true);
		scrolledComposite.setExpandHorizontal(true);

		// tree-viewer
		createTreeViewer(scrolledComposite);
		Tree tree = checkboxTreeViewer.getTree();
		scrolledComposite.setContent(tree);
		scrolledComposite
				.setMinSize(tree.computeSize(SWT.DEFAULT, SWT.DEFAULT));

		// tree-config
		Composite composite_3 = new Composite(sashForm, SWT.NONE);
		composite_3.setLayout(new GridLayout(1, false));

		btnSelectAll = new Button(composite_3, SWT.ALL);
		btnSelectAll.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));
		btnSelectAll.setText(Messages.GeneratorWizardMainPage_Select_all);

		btnDeselectAll = new Button(composite_3, SWT.NONE);
		btnDeselectAll.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));
		btnDeselectAll.setText(Messages.GeneratorWizardMainPage_Deselect_all);

		Label lblFilter = new Label(composite_3, SWT.NONE);
		lblFilter.setText(Messages.GeneratorWizardMainPage_Filter);

		btnNamefilter = new Button(composite_3, SWT.CHECK);
		btnNamefilter.setSelection(true);
		btnNamefilter.setText(Messages.GeneratorWizardMainPage_name);

		btnModifierfilter = new Button(composite_3, SWT.CHECK);
		btnModifierfilter.setText(Messages.GeneratorWizardMainPage_modifier);
		btnModifierfilter.setSelection(true);

		btnExistingMethodsFilter = new Button(composite_3, SWT.CHECK);
		btnExistingMethodsFilter.setText(Messages.GeneratorWizardMainPage_existing_methods);
		btnExistingMethodsFilter.setSelection(true);

		sashForm.setWeights(new int[] { 10, 3 });

		// main settings
		Composite composite_1 = new Composite(mainContainer, SWT.NONE);
		composite_1.setLayout(new FillLayout(SWT.HORIZONTAL));

		Group grpMainSettings = new Group(composite_1, SWT.NONE);
		grpMainSettings.setText(Messages.GeneratorWizardMainPage_main_settings);
		grpMainSettings.setLayout(new FillLayout(SWT.HORIZONTAL));

		Composite composite_2 = new Composite(grpMainSettings, SWT.NONE);
		GridLayout gl_composite_2 = new GridLayout(3, false);
		gl_composite_2.marginWidth = 0;
		gl_composite_2.marginHeight = 0;
		gl_composite_2.horizontalSpacing = 0;
		composite_2.setLayout(gl_composite_2);

		Label lblTestproject = new Label(composite_2, SWT.NONE);
		lblTestproject.setText(Messages.GeneratorWizardMainPage_Testproject);

		txtTestProject = new Text(composite_2, SWT.BORDER);
		txtTestProject.setEditable(false);
		txtTestProject.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));

		Button btnTestProject = new Button(composite_2, SWT.NONE);
		btnTestProject.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));
		btnTestProject.setText(Messages.GeneratorWizardMainPage_Browse);

		Label lblNewLabel = new Label(composite_2, SWT.NONE);
		lblNewLabel.setText(Messages.GeneratorWizardMainPage_Super_class);

		superClass = new Text(composite_2, SWT.BORDER);
		superClass.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));

		Button btnBrowse = new Button(composite_2, SWT.NONE);
		btnBrowse.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));
		btnBrowse.setText(Messages.GeneratorWizardMainPage_Browse);

		Label lblMethodprefix = new Label(composite_2, SWT.NONE);
		lblMethodprefix.setText(Messages.GeneratorWizardMainPage_Testmethod_prefix);

		methodPrefix = new Text(composite_2, SWT.BORDER);
		methodPrefix.setEditable(false);
		methodPrefix.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		new Label(composite_2, SWT.NONE);

		Label lblPrioritt = new Label(composite_2, SWT.NONE);
		lblPrioritt.setText(Messages.GeneratorWizardMainPage_Testpriority);

		Composite composite_4 = new Composite(composite_2, SWT.NONE);
		RowLayout rl_composite_4 = new RowLayout(SWT.HORIZONTAL);
		rl_composite_4.marginTop = 0;
		rl_composite_4.marginRight = 0;
		rl_composite_4.marginLeft = 0;
		rl_composite_4.marginBottom = 0;
		composite_4.setLayout(rl_composite_4);

		btnPrioHigh = new Button(composite_4, SWT.RADIO);
		btnPrioHigh.setText(Messages.GeneratorWizardMainPage_high);

		btnPrioStandard = new Button(composite_4, SWT.RADIO);
		btnPrioStandard.setText(Messages.GeneratorWizardMainPage_standard);
		btnPrioStandard.setSelection(true);

		btnPrioLow = new Button(composite_4, SWT.RADIO);
		btnPrioLow.setText(Messages.GeneratorWizardMainPage_low);
		new Label(composite_2, SWT.NONE);

		Label label = new Label(composite_2, SWT.SEPARATOR | SWT.HORIZONTAL
				| SWT.CENTER);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1,
				1));

		Label label_1 = new Label(composite_2, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1));

		Label label_2 = new Label(composite_2, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1));

		Label lblGenerate = new Label(composite_2, SWT.NONE);
		lblGenerate.setText(Messages.GeneratorWizardMainPage_Generate);
		new Label(composite_2, SWT.NONE);
		new Label(composite_2, SWT.NONE);

		Label lblStandardTestmethods = new Label(composite_2, SWT.NONE);
		lblStandardTestmethods.setText(Messages.GeneratorWizardMainPage_Standardmethods);

		Composite composite_5 = new Composite(composite_2, SWT.NONE);
		GridLayout gl_composite_5 = new GridLayout(2, false);
		gl_composite_5.marginHeight = 0;
		gl_composite_5.verticalSpacing = 0;
		gl_composite_5.marginWidth = 0;
		composite_5.setLayout(gl_composite_5);
		composite_5.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false,
				1, 1));

		btnSetup = new Button(composite_5, SWT.CHECK);
		btnSetup.setText(Messages.GeneratorWizardMainPage_setUp);

		btnTeardown = new Button(composite_5, SWT.CHECK);
		btnTeardown.setText(Messages.GeneratorWizardMainPage_tearDown);

		btnSetupbeforeclass = new Button(composite_5, SWT.CHECK);
		btnSetupbeforeclass.setText(Messages.GeneratorWizardMainPage_setUpBeforeClass);

		btnTeardownafterclass = new Button(composite_5, SWT.CHECK);
		btnTeardownafterclass.setText(Messages.GeneratorWizardMainPage_tearDownAfterClass);

		btnToggleStandardMethods = new Button(composite_2, SWT.NONE);
		btnToggleStandardMethods.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});
		btnToggleStandardMethods.setLayoutData(new GridData(SWT.FILL,
				SWT.CENTER, false, false, 1, 1));
		btnToggleStandardMethods.setText(Messages.GeneratorWizardMainPage_toggle);

		Label lblHookForManuals = new Label(composite_2, SWT.NONE);
		lblHookForManuals.setText(Messages.GeneratorWizardMainPage_Hooks_for_manuals);

		Composite composite_8 = new Composite(composite_2, SWT.NONE);
		GridLayout gl_composite_8 = new GridLayout(2, false);
		gl_composite_8.verticalSpacing = 0;
		gl_composite_8.marginWidth = 0;
		gl_composite_8.marginHeight = 0;
		composite_8.setLayout(gl_composite_8);

		btnCreatetestbasebefore = new Button(composite_8, SWT.CHECK);
		btnCreatetestbasebefore.setText(Messages.GeneratorWizardMainPage_createTestBaseBefore);

		btnBeforemethodcall = new Button(composite_8, SWT.CHECK);
		btnBeforemethodcall.setText(Messages.GeneratorWizardMainPage_beforeMethodCall);
		btnBeforemethodcall.setVisible(false);

		btnCreatetestbaseafter = new Button(composite_8, SWT.CHECK);
		btnCreatetestbaseafter.setText(Messages.GeneratorWizardMainPage_createTestBaseAfter);

		btnAftermethodcall = new Button(composite_8, SWT.CHECK);
		btnAftermethodcall.setText(Messages.GeneratorWizardMainPage_afterMethodCall);

		btnToggleHooks = new Button(composite_2, SWT.NONE);
		btnToggleHooks.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));
		btnToggleHooks.setText(Messages.GeneratorWizardMainPage_toggle);

		Label lblOther = new Label(composite_2, SWT.NONE);
		lblOther.setText(Messages.GeneratorWizardMainPage_Other);

		Composite composite_7 = new Composite(composite_2, SWT.NONE);
		RowLayout rl_composite_7 = new RowLayout(SWT.HORIZONTAL);
		rl_composite_7.marginTop = 0;
		rl_composite_7.marginLeft = 0;
		rl_composite_7.marginRight = 0;
		rl_composite_7.marginBottom = 0;
		composite_7.setLayout(rl_composite_7);

		btnTestsuites = new Button(composite_7, SWT.CHECK);
		btnTestsuites.setText(Messages.GeneratorWizardMainPage_testsuites);

		btnLogger = new Button(composite_7, SWT.CHECK);
		btnLogger.setText(Messages.GeneratorWizardMainPage_logger);

		btnFailassertion = new Button(composite_7, SWT.CHECK);
		btnFailassertion.setText(Messages.GeneratorWizardMainPage_fail_assertions);

		btnToggleOther = new Button(composite_2, SWT.NONE);
		btnToggleOther.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));
		btnToggleOther.setText(Messages.GeneratorWizardMainPage_toggle);
	}

	private CheckboxTreeViewer createTreeViewer(Composite parent) {
		checkboxTreeViewer = new ContainerCheckedTreeViewer(parent, SWT.CHECK);

		ITreeContentProvider contentProvider = new StandardJavaElementContentProvider(true);
		
		checkboxTreeViewer.setContentProvider(contentProvider);
		// label provider
		ILabelProvider labelProvider = new JavaElementLabelProvider(
				JavaElementLabelProvider.SHOW_DEFAULT
						| JavaElementLabelProvider.SHOW_RETURN_TYPE);

		checkboxTreeViewer.setLabelProvider(labelProvider);

		return checkboxTreeViewer;
	}

	public CheckboxTreeViewer getCheckboxTreeViewer() {
		return checkboxTreeViewer;
	}

	public Testprio getTestprio() {
		if (btnPrioHigh.isDisposed() || btnPrioStandard.isDisposed()
				|| btnPrioLow.isDisposed())
			return selectedTestprio;

		if (btnPrioHigh.getSelection()) {
			selectedTestprio = Testprio.HIGH;
		} else if (btnPrioStandard.getSelection()) {
			selectedTestprio = Testprio.DEFAULT;
		} else if (btnPrioLow.getSelection()) {
			selectedTestprio = Testprio.LOW;
		}

		return selectedTestprio;
	}

	@Override
	protected void createController(UTMModel model) {
		this.controller = new GeneratorWizardMain(model, this);
	}

	@Override
	public GeneratorWizardMain getController() {
		return (GeneratorWizardMain) controller;
	}

	public String getGenerateTestsuites() {
		return generateTestsuites;
	}

	public Text getSuperClass() {
		return superClass;
	}

	public Button getBtnNamefilter() {
		return btnNamefilter;
	}

	public Button getBtnModifierfilter() {
		return btnModifierfilter;
	}

	public Button getBtnExistingMethodsFilter() {
		return btnExistingMethodsFilter;
	}

	public Button getBtnFailassertion() {
		return btnFailassertion;
	}

	public Button getBtnLogger() {
		return btnLogger;
	}

	public Text getMethodPrefix() {
		return methodPrefix;
	}

	public Text getTxtTestProject() {
		return txtTestProject;
	}

	public Button getBtnPrioHigh() {
		return btnPrioHigh;
	}

	public Button getBtnPrioStandard() {
		return btnPrioStandard;
	}

	public Button getBtnPrioLow() {
		return btnPrioLow;
	}

	public Vector<IMethod> getMethods() {
		return methods;
	}

	public Testprio getSelectedTestprio() {
		return selectedTestprio;
	}

	public Button getBtnSelectAll() {
		return btnSelectAll;
	}

	public Button getBtnDeselectAll() {
		return btnDeselectAll;
	}

	public Button getBtnSetupbeforeclass() {
		return btnSetupbeforeclass;
	}

	public void setBtnSetupbeforeclass(Button btnSetupbeforeclass) {
		this.btnSetupbeforeclass = btnSetupbeforeclass;
	}

	public Button getBtnTeardown() {
		return btnTeardown;
	}

	public void setBtnTeardown(Button btnTeardown) {
		this.btnTeardown = btnTeardown;
	}

	public Button getBtnTeardownafterclass() {
		return btnTeardownafterclass;
	}

	public void setBtnTeardownafterclass(Button btnTeardownafterclass) {
		this.btnTeardownafterclass = btnTeardownafterclass;
	}

	public Button getBtnToggleStandardMethods() {
		return btnToggleStandardMethods;
	}

	public void setBtnToggleStandardMethods(Button btnToggleStandardMethods) {
		this.btnToggleStandardMethods = btnToggleStandardMethods;
	}

	public Button getBtnTestsuites() {
		return btnTestsuites;
	}

	public void setBtnTestsuites(Button btnTestsuites) {
		this.btnTestsuites = btnTestsuites;
	}

	public Button getBtnToggleOther() {
		return btnToggleOther;
	}

	public void setBtnToggleOther(Button btnToggleOther) {
		this.btnToggleOther = btnToggleOther;
	}

	public Button getBtnToggleHooks() {
		return btnToggleHooks;
	}

	public void setBtnToggleHooks(Button btnToggleHooks) {
		this.btnToggleHooks = btnToggleHooks;
	}

	public Button getBtnBeforemethodcall() {
		return btnBeforemethodcall;
	}

	public void setBtnBeforemethodcall(Button btnBeforemethodcall) {
		this.btnBeforemethodcall = btnBeforemethodcall;
	}

	public Button getBtnAftermethodcall() {
		return btnAftermethodcall;
	}

	public void setBtnAftermethodcall(Button btnAftermethodcall) {
		this.btnAftermethodcall = btnAftermethodcall;
	}

	public Button getBtnSetup() {
		return btnSetup;
	}

	public void setBtnSetup(Button btnSetup) {
		this.btnSetup = btnSetup;
	}

}
